from fastapi import APIRouter, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates

from src.settings import get_settings

html_router = APIRouter(tags=["frontend"])
templates = Jinja2Templates(directory="src/templates")


async def ready_template(
    request: Request,
    method: str,
    endpoint: str,
    path_parametrs: list[str] = [],
    query_parametrs: list[str] = [],
    body_parametrs: list[str] = []
):
    api_url = "http://" + get_settings().BACKEND_LINK
    return templates.TemplateResponse(
        request=request,
        name="endpoint.html",
        context={
            "method": method,
            "link": f"{api_url}/{endpoint}",
            "path_parametrs": path_parametrs,
            "query_parametrs": query_parametrs,
            "body_parametrs": body_parametrs
        }
    )


@html_router.get("/", response_class=HTMLResponse)
async def main_page(request: Request):
    pass


# start USER

@html_router.get("/registration/", response_class=HTMLResponse)
async def registration(request: Request):
    return await ready_template(
        request=request,
        method="POST",
        endpoint="registration",
        body_parametrs=["firstName", "lastName", "email", "password"]
    )


@html_router.get("/login/", response_class=HTMLResponse)
async def login(request: Request):
    return await ready_template(
        request=request,
        method="POST",
        endpoint="login",
        body_parametrs=["email", "password"]
    )


@html_router.get("/accounts/search", response_class=HTMLResponse)
async def search_account(request: Request):
    return await ready_template(
        request=request,
        method="GET",
        endpoint="accounts/search",
        query_parametrs=["firstName", "lastName", "email", "from", "size"]
    )


@html_router.get("/accounts_get", response_class=HTMLResponse)
async def get_account(request: Request):
    return await ready_template(
        request=request,
        method="GET",
        endpoint="accounts/{accountId}/",
        path_parametrs=["accountId"]
    )


@html_router.get("/accounts_put", response_class=HTMLResponse)
async def put_account(request: Request):
    return await ready_template(
        request=request,
        method="PUT",
        endpoint="accounts/{accountId}",
        path_parametrs=["accountId"],
        body_parametrs=["firstName", "lastName", "email", "password"]
    )


@html_router.get("/accounts_delete", response_class=HTMLResponse)
async def delete_account(request: Request):
    return await ready_template(
        request=request,
        method="DELETE",
        endpoint="accounts/{accountId}",
        path_parametrs=["accountId"]
    )

# end USER

# start REGION


@html_router.get("/region_create", response_class=HTMLResponse)
async def create_region(request: Request):
    return await ready_template(
        request=request,
        method="POST",
        endpoint="region",
        body_parametrs=[
            "name",
            "parentRegion",
            "regionType",
            "latitude_1",
            "longitude_1",
            "latitude_2",
            "longitude_2",
            "poi"
        ]
    )


@html_router.get("/region_get", response_class=HTMLResponse)
async def get_region(request: Request):
    return await ready_template(
        request=request,
        method="POST",
        endpoint="region/{regionId}",
        path_parametrs=["regionId"]
    )


@html_router.get("/region_put", response_class=HTMLResponse)
async def put_region(request: Request):
    return await ready_template(
        request=request,
        method="PUT",
        endpoint="region/{regionId}",
        path_parametrs=["regionId"],
        body_parametrs=[
            "name",
            "parentRegion",
            "regionType",
            "latitude_1",
            "longitude_1",
            "latitude_2",
            "longitude_2",
            "poi"
        ]
    )


@html_router.get("/region_delete", response_class=HTMLResponse)
async def delete_region(request: Request):
    return await ready_template(
        request=request,
        method="DELETE",
        endpoint="region/{regionId}",
        path_parametrs=["regionId"]
    )

# end REGION

# start REGION_TYPE


@html_router.get("/region_type_create", response_class=HTMLResponse)
async def create_region_type(request: Request):
    return await ready_template(
        request=request,
        method="POST",
        endpoint="region/types",
        body_parametrs=["type"]
    )


@html_router.get("/region_type_get", response_class=HTMLResponse)
async def get_region_type(request: Request):
    return await ready_template(
        request=request,
        method="GET",
        endpoint="region/types/{type_id}",
        path_parametrs=["type_id"]
    )


@html_router.get("/region_type_put", response_class=HTMLResponse)
async def put_region_type(request: Request):
    return await ready_template(
        request=request,
        method="PUT",
        endpoint="region/types/{type_id}",
        path_parametrs=["type_id"],
        body_parametrs=["type"]
    )


@html_router.get("/region_type_delete", response_class=HTMLResponse)
async def delete_region_type(request: Request):
    return await ready_template(
        request=request,
        method="DELETE",
        endpoint="region/types/{type_id}",
        path_parametrs=["type_id"]
    )

# end REGION_TYPE

# start WEATHER


@html_router.get("/weather_create", response_class=HTMLResponse)
async def create_weather(request: Request):
    return await ready_template(
        request=request,
        method="POST",
        endpoint="region/weather",
        body_parametrs=[
            "regionId",
            "temperature",
            "humidity",
            "windSpeed",
            "weatherCondition",
            "precipitationAmount",
            "measurementDateTime",
            "weatherForecast"
        ]
    )


@html_router.get("/weather_search", response_class=HTMLResponse)
async def search_weather(request: Request):
    return await ready_template(
        request=request,
        method="GET",
        endpoint="region/weather/search",
        query_parametrs=[
            "startDateTime",
            "endDateTime",
            "regionId",
            "weatherCondition",
            "from",
            "zize"
        ]
    )


@html_router.get("/weather_get", response_class=HTMLResponse)
async def get_weather(request: Request):
    return await ready_template(
        request=request,
        method="GET",
        endpoint="region/weather/{regionId}",
        path_parametrs=["regionId"]
    )


@html_router.get("/weather_put", response_class=HTMLResponse)
async def put_weather(request: Request):
    return await ready_template(
        request=request,
        method="PUT",
        endpoint="region/weather/{regionId}",
        path_parametrs=["regionId"],
        body_parametrs=[
            "regionName",
            "temperature",
            "humidity",
            "windSpeed",
            "weatherCondition",
            "precipitationAmount",
            "measurementDateTime",
            "weatherForecast"
        ]
    )


@html_router.get("/weather_delete", response_class=HTMLResponse)
async def delete_weather(request: Request):
    return await ready_template(
        request=request,
        method="DELETE",
        endpoint="region/weather/{regionId}",
        path_parametrs=["regionId"]
    )

# end WEATHER

# start REGION_WEATHER


@html_router.get("/forecast_create", response_class=HTMLResponse)
async def create_forecast(request: Request):
    return await ready_template(
        request=request,
        method="POST",
        endpoint="region/weather/forecast",
        body_parametrs=[
            "dateTime",
            "temperature",
            "weatherCondition",
            "regionId"
        ]
    )


@html_router.get("/forecast_get", response_class=HTMLResponse)
async def get_forecast(request: Request):
    return await ready_template(
        request=request,
        method="GET",
        endpoint="region/weather/forecast/{weatherForecast}",
        path_parametrs=["weatherForecast"]
    )


@html_router.get("/forecast_put", response_class=HTMLResponse)
async def put_forecast(request: Request):
    return await ready_template(
        request=request,
        method="PUT",
        endpoint="region/weather/forecast/{weatherForecast}",
        path_parametrs=["weatherForecast"],
        body_parametrs=[
            "dateTime",
            "temperature",
            "weatherCondition",
            "regionId"
        ]
    )


@html_router.get("/forecast_delete", response_class=HTMLResponse)
async def delete_forecast(request: Request):
    return await ready_template(
        request=request,
        method="DELETE",
        endpoint="region/weather/forecast/{weatherForecast}",
        path_parametrs=["weatherForecast"]
    )


@html_router.get("/map/", response_class=HTMLResponse)
async def get_interactive_map(
    request: Request,
    latitude: float = 64.526369,
    longitude: float = 40.561933
):
    return templates.TemplateResponse(
        request=request,
        name="interactive_map.html",
        context={
            "latitude": latitude,
            "longitude": longitude
        }
    )
